package sn.gandal.agriculture.agrimobile.j2me.gui;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStoreException;
import sn.gandal.agriculture.agrimobile.j2me.utilities.Configuration;

/**
 *
 * @author sowdiomyero
 */
public class GestionAbonneMenu extends List implements CommandListener
{

    private static MIDlet midlet;
    private Display display;
    private Displayable previousScreen;  
    private AddAbonneForm addAbonneForm = null;
    private RechercheAbonneForm rechercheAbonneForm = null;
    private MainMenu mainMenu;
    private Command cmdBack;
    private Command cmdOk;

    public GestionAbonneMenu(Display display)
    {
	super(Configuration.MENU_GESTION_MEMBRES, List.IMPLICIT);

	this.display = display;
	//this.previousScreen = previous;
	this.initialiseCommands();
	this.initialiseComponents();
	this.addCommands();
	this.setCommandListener(this);
	this.setSelectCommand(this.cmdOk);
    }

    private void initialiseComponents()
    {
	this.append(Configuration.MENU_ABONNE_AJOUTER, null);
	this.append(Configuration.MENU_ABONNE_MODIFIER, null);
	this.append(Configuration.MENU_ABONNE_SUPPRIMER, null);
    }

    private void initialiseCommands()
    {
	this.cmdOk = new Command("Ok", Command.ITEM, 1);
	this.cmdBack = new Command("Back", Command.BACK, 2);
    }

    private void addCommands()
    {
	this.addCommand(this.cmdOk);
	this.addCommand(this.cmdBack);
    }

    public void selectAction()
    {
	this.choiceGroupAction();
    }

    public void choiceGroupAction()
    {
	if (this.ajouterAbonne())
	{
	    this.ajouterAbonneAction();
	}
	else if (this.modifierAbonne())
	{

	    this.modifierAbonneAction();
	}
	else if (this.supprimerAbonne())
	{
	    this.supprimerAbonneAction();
	}
    }

    public boolean ajouterAbonne()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_ABONNE_AJOUTER);
    }

    public boolean modifierAbonne()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_ABONNE_MODIFIER);
    }

    public boolean supprimerAbonne()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_ABONNE_SUPPRIMER);
    }

    private void ajouterAbonneAction()
    {
	try
	{
	    this.getAddAbonneForm().initialiseAbonneId();
	}
	catch (RecordStoreException ex)
	{
	    ex.printStackTrace();
	    Alert alert = new Alert("Generer AbonneID", "Une erreur s'est produite. Veuillez re-essayez.", null, AlertType.ERROR);
	    this.display.setCurrent(alert);
	}
	catch (IOException ex)
	{
	    ex.printStackTrace();
	}
	this.display.setCurrent(this.getAddAbonneForm());
    }

    private void modifierAbonneAction()
    {
	this.display.setCurrent(this.getRechercheAbonneForm(Configuration.CMD_MODIFIER));
    }

    private void supprimerAbonneAction()
    {
	this.display.setCurrent(this.getRechercheAbonneForm(Configuration.CMD_SUPPRIMER));
    }

    public boolean equalsCmdOk(Command c)
    {
	return this.cmdOk.equals(c);
    }

    public boolean equalsCmdBack(Command c)
    {
	return this.cmdBack.equals(c);
    }

    public void okAction()
    {
	this.choiceGroupAction();
    }

    private void backAction()
    {

	//this.display.setCurrent(new MainMenu(PesinetMidlet.getMidlet()));

	//this.display.setCurrent(this.previousScreen);
	this.display.setCurrent(this.getMainMenu());

    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdOk))
	{
	    this.okAction();
	}
	else if (c.equals(this.cmdBack))
	{
	    this.backAction();
	}
    }

    private AddAbonneForm getAddAbonneForm()
    {
	if (this.addAbonneForm == null)
	{
	    this.addAbonneForm = new AddAbonneForm(this.display, this);
	}

	return this.addAbonneForm;
    }

    private RechercheAbonneForm getRechercheAbonneForm(String previousItem)
    {
	if (this.rechercheAbonneForm == null)
	{
	    this.rechercheAbonneForm = new RechercheAbonneForm(this.display,previousItem ,this.getTitle());
	}

	return this.rechercheAbonneForm;
    }

     private MainMenu getMainMenu()
    {
	if (this.mainMenu == null)
	{
	    //this.mainMenu = new MainMenu(midlet);
	    this.mainMenu = new MainMenu(mainMenu.getMidlet());
	}

	return this.mainMenu;
    }
}
